/*****************************************************************************
 *                                                                           *
 *   Copyright (C) 2010 by Luca Cireddu                                      *
 *                         luca@lucacireddu.it                               *
 *                         http://www.lucacireddu.it                         *
 *                                                                           *
 *   This program is free software; you can redistribute it and/or modify    *
 *   it under the terms of the GNU General Public License as published by    *
 *   the Free Software Foundation; either version 2 of the License.          *
 *                                                                           *
 *   This program is distributed in the hope that it will be useful,         *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with this program; if not, write to the                           *
 *   Free Software Foundation, Inc.                                          *
 *   51 Franklin Street, Fifth Floor                                         *
 *   Boston, MA  02110-1301, USA.                                            *
 *                                                                           *
 *****************************************************************************/

#include <stdlib.h>
#include <malloc.h>
#include <getopt.h>
#include <string.h>

#include "common.h"
#include "defines.h"
#include "ngexit.h"
#include "parseargs.h"
#include "version.h"

extern graph_param params;
extern int grph_x;
extern int grph_y;

bool parseArgs(int argc, char **argv)
{
    bool be_an_helper;
    bool be_an_version;
    bool be_an_japanese;

    char *cmdline;
    char option;

    static struct option long_options[] = {
        {"help", no_argument, 0, 'h'},
        {"version", no_argument, 0, 'V'},
        {"verbose", no_argument, 0, 'v'},
        {"quiet", no_argument, 0, 'q'},
        {"device", required_argument, 0, 'd'},
        {"output", required_argument, 0, 'o' },
        {"interval", required_argument, 0, 'i'},
        {"storage", required_argument, 0, 's'},
        {"dim-x", required_argument, 0, 'x'},
        {"dim-y", required_argument, 0, 'y'},
        {"max-rx", required_argument, 0, 'r'},
        {"max-tx", required_argument, 0, 't'},
        {0, 0, 0, 0}
    };

    params.devname = "wlan0";
    params.datastorage = "tmp/datastorage";
    params.interval = 1;
    params.outfile = "tmp/wlan0.png";
    params.outx = 400;
    params.outy = 100;
    params.max_rx = 750;
    params.max_tx = 70;
    params.verbose = B_FALSE;

    if(argc == 1)
        return B_TRUE;

    be_an_helper = B_FALSE;
    be_an_version = B_FALSE;
    be_an_japanese = B_FALSE;

    do {
        option = getopt_long(argc, argv, "hVvqd:o:i:s:x:y:r:t:j", long_options, NULL);

        if(option == 'h')
            be_an_helper = B_TRUE;

        if(option == 'V')
            be_an_version = B_TRUE;

        if(option == 'v')
            params.verbose = B_TRUE;

        if(option == 'q')
            params.verbose = B_FALSE;

        if(option == 'd')
            params.devname = optarg;

        if(option == 'o')
            params.outfile = optarg;

        if(option == 'i')
            params.interval = atoi(optarg);

        if(option == 's')
            params.datastorage = optarg;

        if(option == 'x')
            params.outx = atoi(optarg);

        if(option == 'y')
            params.outy = atoi(optarg);

        if(option == 'r')
            params.max_rx = atoi(optarg);

        if(option == 't')
            params.max_tx = atoi(optarg);

        if(option == 'j')
            be_an_japanese = B_TRUE;

    } while(option != -1);

    if(be_an_japanese == B_TRUE) {
        printf("だってばよ\n");
        return B_FALSE;
    }

    if(be_an_helper == B_TRUE) {
        cmdline = implodeCmdLine(argc, argv);
        showHelp(argv[0], cmdline);
        free(cmdline);
        return B_FALSE;
    }

    if(be_an_version == B_TRUE) {
        showVersion();
        return B_FALSE;
    }

    grph_x = params.outx - (BORDER_LEFT + BORDER_RIGHT + 2);
    grph_y = params.outy - (BORDER_TOP + BORDER_BOTTOM + 2);

    return B_TRUE;
}

char *implodeCmdLine(int argc, char **argv)
{
    char *ret;
    int i, ln;

    if(argc == 1) {
        ret = (char *) malloc((strlen(argv[0]) + 1) * sizeof(char));
        strcpy(ret, argv[0]);
        return ret;
    }

    ret = (char *) malloc(sizeof(char));
    *(ret) = '\0';

    for(i = 0; i < argc; i++) {
        if(i > 0) {
            ln = strlen(ret);
            ret = (char *) realloc(ret, (ln + 2) * sizeof(char));
            strcat(ret, " ");
        }

        ln = strlen(ret);
        ret = (char *) realloc(ret, (ln + strlen(argv[i]) + 1) * sizeof(char));
        strcat(ret, argv[i]);
    }

    return ret;
}

void showHelp(char *name, char *cmdline)
{
    printf("\n");
    printf("COMMAND: %s\n", cmdline);
    printf("\n");
    printf("Usage of %s\n", name);
    printf("\n");
    printf("  -h | --help        Show this message\n");
    printf("  -V | --version     Show version informations\n");
    printf("\n");

    if(params.verbose == B_TRUE) {
        printf("  -v | --verbose     Be verbose (default)\n");
        printf("  -q | --quite       Be quite\n");
    } else {
        printf("  -v | --verbose     Be verbose\n");
        printf("  -q | --quite       Be quite (default)\n");
    }

    printf("\n");
    printf("  -d | --device      Set device to analyse [%s]\n", params.devname);
    printf("  -o | --output      Set the output path and/or filename for the image [%s]\n", params.datastorage);
    printf("  -i | --interval    Set the refresh interval in seconds [%d]\n", params.interval);
    printf("  -s | --storage     Set the output path and/or filename for the data file [%s]\n", params.outfile);
    printf("  -x | --dim-x       Set the horizontal size of the output image [%d]\n", params.outx);
    printf("  -y | --dim-y       Set the vertical size of the output image [%d]\n", params.outy);
    printf("  -r | --max-rx      Set maximum RX value in KB/s of the graph [%d]\n", params.max_rx);
    printf("  -t | --max-tx      Set maximum TX value in KB/s of the graph [%d]\n", params.max_tx);
    printf("\n");
    printf("\n");
}

void showVersion()
{
    printf("\n");
    printf("%s ver. %s\n", netgraph_name, netgraph_version);
    printf("\n");
    printf("\n");
}
